from filecmp import cmp
from operator import index

# 参考：https://www.imooc.com/code/21976

# 指定{}的名字w,c,b,i
template = 'Hello {w}, Hello {c}, Hello {b}, Hello {i}.'
world = 'World'
china = 'China'
beijing = 'Beijing'
imooc = 'imooc'
# 指定名字对应的模板数据内容
result = template.format(w = world, c = china, b = beijing, i = imooc)
print(result) # ==> Hello World, Hello China, Hello Beijing, Hello imooc.


s1='这是中文字符串'
s2='this is an English string'
print(s1)
print(s2)


#python字符串切片
s='ABC'
a=s[0]
b=s[1]
c=s[2]
print(a)
print(b)
print(c)

ab=s[0:2]
print(ab)
s='ABCDEFGHIJK'
abcd=s[0:4]
print(abcd)
cdef=s[2:6]
print(cdef)

score=59
if score>=90:
    print('A')
else:
    print('B')

s='ABCD'
for ch in s:
    print(ch)

L = [75, 92, 59, 68, 99]
retSum=0
for xiaol in L:
    retSum+=xiaol
print(retSum)


num=1
sum=0
while num<=100:
     sum+=num
     num=num+1
print(sum)


num = 1
sum = 0
while True:
    if num>100:
        break
    sum=sum+num
    num=num+1
print(sum)


s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
num = 1
for ch in s:
    if num > 20:
        break
    print(ch)
    num = num + 1

# python continue 继续循环
s = 'ABCDEFGHIJKLMNOPQRSTUVXYZ'
num=1
for ch in s:
    if num<10:
        num=num+1
        continue
    print(ch)
    num=num+1

# python 嵌套循环
s1='ABC'
s2='123'
for x in s1:
    for y in s2:
         print(x+y)
# list
scores=[45,60,75,86,49,100]
print(scores)
names = ['Alice', 'Bob', 'David', 'Ellena']
print(names)
L = ['Alice', 66, 'Bob', True, 'False', 100]
print(L)

# 顺序访问list
L=['Alice', 66, 'Bob', True, 'False', 100]
for x in L:
    print(x)


# 按索引访问list
    names=['Alice', 'Bob', 'David', 'Ellena']
    print(names[0])
    print(names[1])
    print(names[2])
    print(names[3])

    sub_names=names[0:2]
    print(sub_names)
    sub_names=names[5:10]
    print(sub_names)

#  倒序访问list
names=['Alice', 'Bob', 'David', 'Ellena']
print(names[-1])
print(names[-2])
print(names[-3])

#  python向list 添加新的元素
names=['Alice', 'Bob', 'David', 'Ellena']


#  Python向list 添加新的元素
names=['Alice', 'Bob', 'David', 'Ellena']
names.append('Candy')
print(names)

names.insert(2, 'Candy')
print(names)


# Python 删除元素
L=['Alice', 'Bob', 'David', 'Ellena']
name=L.pop()
print(name)
print(L)

name=L.pop(2)
print(name)
print(L)

# Python 替换元素

L=['Alice', 'Bob', 'David', 'Ellena']
L[2]='Canlina'
print(L)
L[-3]='Canlina'
print(L)

# Python 二维list
alice_scores=[100,89,92]
bob_scores=[70,65,81]
candy_scores=[88,72,77]

all_scores=[alice_scores,bob_scores,candy_scores]

print(all_scores)
# bobo 最近第三次考试的成绩
print(all_scores[1][2])

#元组
# Enter a code
T=('Alice','Bob','Candy','David','Ellena')

# 通过下标的方式访问元素
print(T[0])
print(T[4])
# 切片
print(T[1:3])

# 列表转元组
L=['Alicem','Bob','David','Ellena']
print(L)

T=tuple(L)
print(T)

# 元组转列表
L=list(T)
print(L)

#  tuple 和list  不一样，tuple 是固定不变的，tuple每一个元素都不可被改变


# 6-2 访问tuple元素的其他方法
T=(1,1,2,2,3,3,1,3,5,7,9)
print(T.count(1))
print(T.count(5))
print(T.count(100))

# index 方法
print(T.index(9))
print(T.index(5))

T=()
print(T)
T=(1)
print(T)

result=3*4-2
print(result)

T=(1,)
print(T)

T=(1,2,3,)
print(T)

T=((1+2),((1+2),),('a'+'b'),(1,),(1,2,3,4,5))
print(T)
print(len(T))



# Python 的可变tuple
T=(1,'CH',[3,4])
L=T[2]
print(L)
L[1]=40
print(T)


#  dict
names=['Alice', 'Bob', 'David', 'Ellena']
scores=[45,60,75,86,49]

index=0

for name in names:
      score=scores[index]
      print('name={},score={}'.format(name,score))
      index+=1


d={
    'Alice':45,
    'Bob':60,
    'Candy':75,
    'David':86,
    'Ellena':49
}

print(d)
d['Gaven']=86
print(d)
print(d.get('Gaven'))

# dict 添加元素
td={}
td['Mini']=[72,73]
td['Dodo']=[88,90]
print(td)

td['Mini'].append(100)
print(td)

# 更新元素
d = {
    'Alice': 45,
    'Bob': 60,
    'Candy': 75,
    'David': 86,
    'Ellena': 49
}
d['Bob'] = 75
print(d)
# ==> {'Alice': 45, 'Bob': 75, 'Candy': 75, 'David': 86, 'Ellena': 49}

# 删除元素
d.pop('Bob')
print(d)

d = {
    'Alice': 45,
    'Bob': 60,
    'Candy': 75,
    'David': 86,
    'Ellena': 49
}
for key in d: # 遍历d的key
    value = d[key]
    if value > 60:
        print(key, value)
# ==> Candy 75
# ==> David 86


for key,value in d.items(): # 遍历d的key
    if value > 60:
        print(key, value)
# ==> Candy 75
# ==> David 86

# set 只有key  不能重复
s=set([1,4,3,2,5])
print(s)


names=['Alice', 'Bob', 'David', 'Ellena']
name_set=set(names)
name_set.add('Gina')

new_names=['Hally','Isen','Jenny','Karl']
name_set.update(new_names)

# 删除元素，没有元素会报错
name_set.remove('Karl')

# 删除元素 没有元素不会报错
name_set.discard('Jenny')
print(name_set)
print('Alice2' in name_set)

# 集合的子集和 超集
s1=set([1,2,3,4,5])
s2=set([1,2,3,4,5,6,7,8,9])



# 判断s1是否为s2的子集
print(s1.issubset(s2))
# 判断s2 是否为s1的超集
print(s2.issuperset(s1))

# 判断s1,s2 是否重合
print(s2.isdisjoint(s1))

# Python 添加set 元素

# 函数
print(cmp(1,2))

def my_abs(x):
     if x>0:
        return x
     else:
         return -x
print(my_abs(-2))


def data_of_square(side):
    C=4*side
    S=side*side
    return  C ,S

C,S=data_of_square(16)
# 周长=64
print('周长={}'.format(C))
# 面积 256
print('面积={}'.format(S))


# 递归
def fact(n):
     if n==1:
         return 1
     return n*fact(n-1)

print(fact(4))


# 参数校验
def my_abs_valid(x):
    if not isinstance(x,int ) or not isinstance(x,float):
        print('param type error.')
        return None
    if x>=0:
        return x
    else:
        return -x

print(my_abs_valid('xxxx'))


# 默认参数
def power(x, n=2):
    s = 1
    while n > 0:
        n = n - 1
        s = s * x
    return s
print(power(2))